/*
 * @(#)MaintFormElementForm.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.form.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.dcivision.form.FormConstant;
import com.dcivision.form.bean.FormElement;
import com.dcivision.form.bean.FormSelectionFieldOption;
import com.dcivision.form.bean.MtmFormSectionFormElement;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.web.AbstractActionForm;

/**
 * MaintFormElementForm.java
 * 
 * This class is the for web form purpose.
 * 
 * @author Jamin Wang
 * @company DCIVision Limited
 * @creation date 18/1/2004
 * @version $Revision: 1.25.2.1 $
 */

public class MaintFormElementForm extends AbstractActionForm {

  public static final String REVISION = "$Revision: 1.25.2.1 $";

  private String elementType = null;

  private String dataType = null;

  private String defaultValue = null;

  private String sqlField = null;

  private String sqlTable = null;

  private String conditionID = null;

  private String sqlWhere = null;

  private String compareSymbol = null;

  private String defaultStyle = null;

  private String fieldName = null;

  private String fieldCaption = null;

  private String isLabel = null;

  private String width = null;

  private String height = null;

  private String expandable = null;

  private String requiredFlag = null;

  private String validationRule = null;

  private String uniqueness = null;

  private String formSectionID = null;

  private FormFile uploadFile = null;

  private String fileName = null;

  private String formElementName = null;

  private String formElementType = null;

  private String mandatory = null;

  private String isDelete = null;

  private String labelPosition = null;

  private String fontType = null;

  private String fontSize = null;

  private String fontColor = null;

  private String fontStyleBold = null;

  private String fontStyleUnderline = null;

  private String fontStyleItalic = null;

  private String fontStyle = null;

  private String displayType = null;

  private List optionList = new ArrayList();

  private String[] selectedSqlResult;

  private String referencFormats = null;

  private String displayFormat = null;

  public MaintFormElementForm() {
    super();
  }

  public String getformElementType() {
    return (this.formElementType);
  }

  public void setformElementType(String formElementType) {
    this.formElementType = formElementType;
  }

  public String getmandatory() {
    return (this.mandatory);
  }

  public void setmandatory(String mandatory) {
    this.mandatory = mandatory;
  }

  public String getformElementName() {
    return (this.formElementName);
  }

  public void setformElementName(String formElementName) {
    this.formElementName = formElementName;
  }

  public String getElementType() {
    return (this.elementType);
  }

  public void setElementType(String elementType) {
    this.elementType = elementType;
  }

  public String getDataType() {
    return (this.dataType);
  }

  public void setDataType(String dataType) {
    this.dataType = dataType;
  }

  public String getDefaultValue() {
    return (this.defaultValue);
  }

  public void setDefaultValue(String defaultValue) {
    this.defaultValue = defaultValue;
  }

  public String getSqlField() {
    return this.sqlField;
  }

  public void setSqlField(String fieldName) {
    this.sqlField = fieldName;
  }

  public String getSqlTable() {
    return this.sqlTable;
  }

  public void setSqlTable(String tableName) {
    this.sqlTable = tableName;
  }

  public String getConditionID() {
    return this.conditionID;
  }

  public void setConditionID(String id) {
    this.conditionID = id;
  }

  public String getSqlWhere() {
    return this.sqlWhere;
  }

  public void setSqlWhere(String condition) {
    this.sqlWhere = condition;
  }

  public String getCompareSymbol() {
    return this.compareSymbol;
  }

  public void setCompareSymbol(String symbol) {
    this.compareSymbol = symbol;
  }

  public String getDefaultStyle() {
    return (this.defaultStyle);
  }

  public void setDefaultStyle(String defaultStyle) {
    this.defaultStyle = defaultStyle;
  }

  public FormFile getUploadFile() {
    return (this.uploadFile);

  }

  public void setUploadFile(FormFile uploadFile) {
    this.uploadFile = uploadFile;
  }

  public String getFieldName() {
    return (this.fieldName);
  }

  public void setFieldName(String fieldName) {
    this.fieldName = fieldName;
  }

  public String getFieldCaption() {
    return (this.fieldCaption);
  }

  public void setFieldCaption(String fieldCaption) {
    this.fieldCaption = fieldCaption;
  }

  public String getIsLabel() {
    return isLabel;
  }

  public void setIsLabel(String isLabel) {
    this.isLabel = isLabel;
  }

  public String getWidth() {
    return (this.width);
  }

  public void setWidth(String width) {
    this.width = width;
  }

  public String getHeight() {
    return (this.height);
  }

  public void setHeight(String height) {
    this.height = height;
  }

  public String getExpandable() {
    return (this.expandable);
  }

  public void setExpandable(String expandable) {
    this.expandable = expandable;
  }

  public String getRequiredFlag() {
    return (this.requiredFlag);
  }

  public void setRequiredFlag(String requiredFlag) {
    this.requiredFlag = requiredFlag;
  }

  public String getValidationRule() {
    return (this.validationRule);
  }

  public void setValidationRule(String validationRule) {
    this.validationRule = validationRule;
  }

  public String getUniqueness() {
    return uniqueness;
  }

  public void setUniqueness(String uniqueness) {
    this.uniqueness = uniqueness;
  }

  public String getFormSectionID() {
    return (this.formSectionID);
  }

  public void setFormSectionID(String formSectionID) {
    this.formSectionID = formSectionID;
  }

  public String getLabelPosition() {
    return (this.labelPosition);
  }

  public void setLabelPosition(String labelPosition) {
    this.labelPosition = labelPosition;
  }

  public String getFontType() {
    return (this.fontType);
  }

  public void setFontType(String fontType) {
    this.fontType = fontType;
  }

  public String getFontColor() {
    return (this.fontColor);
  }

  public void setFontColor(String fontColor) {
    this.fontColor = fontColor;
  }

  public String getFontSize() {
    return (this.fontSize);
  }

  public void setFontSize(String fontSize) {
    this.fontSize = fontSize;
  }

  public String getFontStyle() {
    return this.fontStyle;
  }

  public void setFontStyle(String fontStyle) {
    this.fontStyle = fontStyle;
  }

  public String getFontStyleBold() {
    return this.fontStyleBold;
  }

  public void setFontStyleBold(String fontStyleBold) {
    this.fontStyleBold = fontStyleBold;
  }

  public String getFontStyleUnderline() {
    return this.fontStyleUnderline;
  }

  public void setFontStyleUnderline(String fontStyleUnderline) {
    this.fontStyleUnderline = fontStyleUnderline;
  }

  public String getFontStyleItalic() {
    return this.fontStyleItalic;
  }

  public void setFontStyleItalic(String fontStyleItalic) {
    this.fontStyleItalic = fontStyleItalic;
  }

  public String getDisplayType() {
    return this.displayType;
  }

  public void setDisplayType(String displayType) {
    this.displayType = displayType;
  }

  public List getOptionList() {
    boolean isEmptyOfLabel;
    boolean isDeleteOfOption;
    for (int i = 0; i >= 0 && i < optionList.size(); i++) {
      isEmptyOfLabel=Utility.isEmpty(((FormSelectionFieldOption) optionList.get(i)).getLabel());
      isDeleteOfOption="Y".equals(((FormSelectionFieldOption) optionList.get(i)).getIsDelete());
      if (isEmptyOfLabel || isDeleteOfOption) {
        optionList.remove(i);
        i--;
      }
    }
    return optionList;
  }

  public List getAllOptionList() {
    return optionList;
  }

  /**
   * Getter for property fileName.
   * 
   * @return Value of property fileName.
   * 
   */
  public String getFileName() {
    return this.fileName;
  }

  /**
   * Setter for property fileName.
   * 
   * @param fileName
   *          New value of property fileName.
   * 
   */
  public void setFileName(String fileName) {
    this.fileName = fileName;
  }

  public void setOptionList(List optionList) {
    this.optionList.addAll(optionList);
  }

  public void setSelectedSqlResult(String[] list) {
    this.selectedSqlResult = list;
  }

  public String[] getSelectedSqlResult() {
    return this.selectedSqlResult;
  }

  public void setReferencFormats(String format) {
    this.referencFormats = format;
  }

  public String getReferencFormats() {
    return this.referencFormats;
  }

  public void setDisplayFormat(String str) {
    this.displayFormat = str;
  }

  public String getDisplayFormat() {
    return this.displayFormat;
  }

  public FormSelectionFieldOption getOption(int index) {
    while (optionList.size() <= index) {
      FormSelectionFieldOption option = new FormSelectionFieldOption();
      option.setSequence(new Integer(optionList.size()));
      optionList.add(option);
    }
    return (FormSelectionFieldOption) optionList.get(index);
  }

  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    return super.validate(mapping, request);
  }

  public void reset(ActionMapping mapping, HttpServletRequest request) {
    super.reset(mapping, request);
    this.setID(null);
    this.setElementType(null);
    this.setDataType(null);
    this.setDefaultValue(null);
    this.setSqlField(null);
    this.setSqlTable(null);
    this.setSqlWhere(null);
    this.setConditionID(null);
    this.setCompareSymbol(null);
    this.setDefaultStyle(null);
    this.setFieldName(null);
    this.setFileName(null);
    this.setUploadFile(null);
    this.setFieldCaption(null);
    this.setWidth(null);
    this.setHeight(null);
    this.setExpandable(null);
    this.setRequiredFlag(null);
    this.setValidationRule(null);
    this.setUniqueness(null);
    this.setFormSectionID(null);
    this.setRecordStatus(null);
    this.setUpdateCount(null);
    this.setCreatorID(null);
    this.setCreateDate(null);
    this.setUpdaterID(null);
    this.setUpdateDate(null);
  }

  public void OpReset(ActionMapping mapping, HttpServletRequest request) {
    super.reset(mapping, request);
    this.setID(null);
    this.setElementType(null);
    this.setDataType(null);
    this.setDefaultValue(null);
    this.setSqlField(null);
    this.setSqlTable(null);
    this.setSqlWhere(null);
    this.setConditionID(null);
    this.setCompareSymbol(null);
    this.setDefaultStyle(null);
    this.setFieldName(null);
    this.setFileName(null);
    this.setUploadFile(null);
    this.setFieldCaption(null);
    this.setWidth(null);
    this.setHeight(null);
    this.setExpandable(null);
    this.setRequiredFlag(null);
    this.setValidationRule(null);
    this.setUniqueness(null);
    this.setFormSectionID(null);
    this.setRecordStatus(null);
    this.setUpdateCount(null);
    this.setCreatorID(null);
    this.setCreateDate(null);
    this.setUpdaterID(null);
    this.setUpdateDate(null);
  }

  public AbstractBaseObject getFormData() throws ApplicationException {
    FormElement tmpFormElement = new FormElement();
    tmpFormElement.setID(TextUtility.parseIntegerObj(this.getID()));
    tmpFormElement.setElementType(this.getElementType());
    tmpFormElement.setDataType(this.getDataType());
    tmpFormElement.setDefaultValue(this.getDefaultValue());
    tmpFormElement.setDefaultStyle(TextUtility.parseIntegerObj(this.getDefaultStyle()));
    tmpFormElement.setFieldName(this.getFieldName());
    tmpFormElement.setFileName(this.getFileName());
    tmpFormElement.setIsLabel(this.getIsLabel());
    tmpFormElement.setFieldCaption(this.getFieldCaption());
    tmpFormElement.setWidth(TextUtility.parseIntegerObj(this.getWidth()));
    tmpFormElement.setHeight(TextUtility.parseIntegerObj(this.getHeight()));
    tmpFormElement.setExpandable(TextUtility.parseIntegerObj(this.getExpandable()));
    tmpFormElement.setLabelPosition(this.getLabelPosition());
    tmpFormElement.setFontType(this.getFontType());
    tmpFormElement.setFontColor(this.getFontColor());
    tmpFormElement.setFontSize(TextUtility.parseIntegerObj(this.getFontSize()));
    tmpFormElement.setFontStyle(this.getFontStyle());
    tmpFormElement.setRequiredFlag(this.getRequiredFlag());
    tmpFormElement.setValidationRule(this.getValidationRule());
    tmpFormElement.setUniqueness(this.getUniqueness());
    tmpFormElement.setRecordStatus(this.getRecordStatus());
    tmpFormElement.setUpdateCount(TextUtility.parseIntegerObj(this.getUpdateCount()));
    tmpFormElement.setCreatorID(TextUtility.parseIntegerObj(this.getCreatorID()));
    tmpFormElement.setCreateDate(parseTimestamp(this.getCreateDate()));
    tmpFormElement.setUpdaterID(TextUtility.parseIntegerObj(this.getUpdaterID()));
    tmpFormElement.setUpdateDate(parseTimestamp(this.getUpdateDate()));
    return tmpFormElement;
  }

  public void setFormData(AbstractBaseObject baseObj) throws ApplicationException {
    FormElement tmpFormElement = (FormElement) baseObj;
    this.setID(TextUtility.formatIntegerObj(tmpFormElement.getID()));
    this.setElementType(tmpFormElement.getElementType());
    this.setDataType(tmpFormElement.getDataType());
    this.setDefaultValue(tmpFormElement.getDefaultValue());
    if (tmpFormElement.getDynamciSql() != null && !(Utility.isEmpty(tmpFormElement.getDynamciSql().getSqlStatement()))) {
      String sql = tmpFormElement.getDynamciSql().getSqlStatement();
      log.debug("SQLStatement=" + sql);
      String[] array = sql.split(FormConstant.FORM_DB_LOOKUP_SQL_SEPARATOR);
      log.debug("SQLArray.length=" + array.length);
      String sqlField = array[1];
      log.debug("SQLField=" + sqlField);
      String sqlTable = array[3];
      log.debug("SQLTable=" + sqlTable);
      String sqlWhere = array[5];
      log.debug("SQLWhere=" + sqlWhere);
      String sybol = array[6];
      log.debug("SQLCompareSybol=" + sybol);
      this.setSqlField(sqlField);
      this.setSqlTable(sqlTable);
      this.setSqlWhere(sqlWhere);
      this.setCompareSymbol(sybol);
      this.setConditionID(tmpFormElement.getDynamciSql().getConditionElementID().toString());
      log.debug("conditionID=" + this.getConditionID());
    }
    this.setDefaultStyle(TextUtility.formatIntegerObj(tmpFormElement.getDefaultStyle()));
    this.setFieldName(tmpFormElement.getFieldName());
    this.setIsLabel(tmpFormElement.getIsLabel());
    this.setFileName(tmpFormElement.getFileName());
    this.setFieldCaption(tmpFormElement.getFieldCaption());
    this.setWidth(TextUtility.formatIntegerObj(tmpFormElement.getWidth()));
    this.setHeight(TextUtility.formatIntegerObj(tmpFormElement.getHeight()));
    this.setExpandable(TextUtility.formatIntegerObj(tmpFormElement.getExpandable()));
    this.setLabelPosition(tmpFormElement.getLabelPosition());
    this.setFontType(tmpFormElement.getFontType());
    this.setFontColor(tmpFormElement.getFontColor());
    this.setFontSize(TextUtility.formatIntegerObj(tmpFormElement.getFontSize()));
    this.setFontStyle(tmpFormElement.getFontStyle());
    this.setRequiredFlag(tmpFormElement.getRequiredFlag());
    this.setValidationRule(tmpFormElement.getValidationRule());
    this.setUniqueness(tmpFormElement.getUniqueness());
    this.setRecordStatus(tmpFormElement.getRecordStatus());
    this.setUpdateCount(TextUtility.formatIntegerObj(tmpFormElement.getUpdateCount()));
    this.setCreatorID(TextUtility.formatIntegerObj(tmpFormElement.getCreatorID()));
    this.setCreateDate(formatTimestamp(tmpFormElement.getCreateDate()));
    this.setUpdaterID(TextUtility.formatIntegerObj(tmpFormElement.getUpdaterID()));
    this.setUpdateDate(formatTimestamp(tmpFormElement.getUpdateDate()));

  }

  public AbstractBaseObject getMtmData() throws ApplicationException {
    MtmFormSectionFormElement tmpMtmMtmFormSectionFormElement = new MtmFormSectionFormElement();
    tmpMtmMtmFormSectionFormElement.setSectionID(TextUtility.parseIntegerObj(this.getFormSectionID()));
    tmpMtmMtmFormSectionFormElement.setElementID(TextUtility.parseIntegerObj(this.getID()));
    return tmpMtmMtmFormSectionFormElement;
  }

  public void setMtmData(AbstractBaseObject baseObj) throws ApplicationException {
    MtmFormSectionFormElement tmpMtmMtmFormSectionFormElement = (MtmFormSectionFormElement) baseObj;
  }

}
